<?php
/**
 * 商品推荐控制器
 * Created by PhpStorm.
 * User: Jmashimaro
 * Date: 2015/2/11
 * Time: 12:00
 */
class RecommendApp extends FrontendApp {

    /**
     * 获取推荐列表
     * @author jiayanxiu
     * @date 2015/02/11
     */
    public function index() {
        $method = strtoupper($_SERVER['REQUEST_METHOD']);
        if ($method !== 'GET') {
            $this->ajax_return(FLAG_ERROR, '非法的请求！');
        }
        $config = require ROOT_PATH.'/data/config.qiniu.php';
        $tmp = $data = $row = array();
        $type = $this->get_gp('type', 'G');
        if ($type !== 'star' && $type !== 'editor') {
            $type = 'editor';
        }
        $offset = $this->get_gp('offset', 'G');
        $size = $this->get_gp('size', 'G');
        $offset = $offset ? intval($offset) : 0;
        $size = $size ? intval($size) : 10;
        //获取明星推荐的信息
        $model = m('recommend');
        $sql = "SELECT recom_id FROM ".DB_PREFIX."recommend WHERE type='{$type}' ORDER BY recom_id DESC LIMIT {$offset},{$size}";
        $stars = $model->getAll($sql);
        $recom_id = '';
        if ($stars) {
            foreach ($stars as $key=>$val) {
                $recom_id .= $val['recom_id'].',';
            }
        } else {
            $this->ajax_return(FLAG_ERROR, '没有更多数据！');
        }
        $recom_id = rtrim($recom_id, ',');
        $sql = "SELECT r.recom_id,r.recom_name,r.recom_title,r.recom_content,r.type,r.recom_image,rg.goods_id
                FROM ".DB_PREFIX."recommend r LEFT JOIN ".DB_PREFIX."recommend_goods rg
                ON r.recom_id=rg.recom_id WHERE r.recom_id IN ({$recom_id}) ORDER BY r.recom_id DESC";
        $tmp = $model->getAll($sql);
        //获取推荐的商品信息
        if ($tmp) {
            foreach ($tmp as $key=>$val) {
                $row[$val['recom_id']]['recom_id'] = $val['recom_id'];
                $row[$val['recom_id']]['recom_name'] = $val['recom_name'];
                $row[$val['recom_id']]['recom_title'] = $val['recom_title'];
                $row[$val['recom_id']]['recom_content'] = $val['recom_content'];
                $row[$val['recom_id']]['type'] = $val['type'];
                $row[$val['recom_id']]['recom_image'] = $val['recom_image'] ? $config['qiniu_url'].$val['recom_image'] : '';
                $row[$val['recom_id']]['goods_id'] .= $val['goods_id'].',';
            }
        }
        //获取推荐的商品信息
        if ($row) {
            foreach ($row as $k=>$v) {
                $val['goods_id'] = trim($v['goods_id'], ',');
                //如果存在商品则去取数据
                if ($val['goods_id']) {
                    $sql = "SELECT goods_id,store_id,goods_name,introduction,default_image,price FROM ".DB_PREFIX."goods
                            WHERE goods_id IN ({$val['goods_id']}) AND if_examine = 1 AND if_finance = 1 AND if_show = 1
                            AND closed = 0 ORDER BY last_update DESC LIMIT 10;";
                    $info = $model->getAll($sql);
                    if ($info) {
                        foreach ($info as $keys=>$value) {
                            $value['introduction'] = stripslashes($value['introduction']);
                            $value['default_image'] = $value['default_image'] ? $config['qiniu_url'].$value['default_image'] : '';
                            $info[$keys] = $value;
                        }
                    }
                    $v['goods'] = $info;
                }
                $data[] = $v;
            }
        }
        $this->ajax_return(FLAG_OK, '', $data);
    }


    /**
     * 单个推荐列表
     * @author jiayanxiu
     * @date 2015/02/01
     */
    public function singleList() {
        $config = require ROOT_PATH.'/data/config.qiniu.php';
        $recom_id = $this->get_gp('recom_id', 'G');
        $offset = $this->get_gp('offset', 'G');
        $size = $this->get_gp('size', 'G');
        $offset = $offset ? intval($offset) : 0;
        $size = $size ? intval($size) : 10;
        $user_id = $this->checkLogin();
        $sql = "SELECT rg.recom_id,g.goods_id,g.goods_name,g.store_id,g.price,g.default_image,g.price,g.introduction,gs.collects
                FROM ".DB_PREFIX."recommend_goods rg LEFT JOIN ".DB_PREFIX."goods g ON rg.goods_id=g.goods_id
                LEFT JOIN ".DB_PREFIX."goods_statistics gs ON gs.goods_id=g.goods_id WHERE rg.recom_id={$recom_id} AND if_examine = 1
                AND if_finance = 1 AND if_show = 1 AND closed = 0 ORDER BY rg.recom_id DESC LIMIT {$offset},{$size}";
        $model = m('recommend');
        $data = $model->getAll($sql);
        if ($data) {
            foreach ($data as $key=>$val) {
                //是否关注了此商品
                if ($user_id) {
                    $sql = "SELECT user_id FROM ".DB_PREFIX."collect WHERE user_id={$user_id} AND item_id={$val['goods_id']} AND `type`='goods'";
                    $row = $model->getRow($sql);
                    if ($row) {
                        $val['follow'] = 1;
                    } else {
                        $val['follow'] = 0;
                    }
                } else {
                    $val['follow'] = 0;
                }
                $val['introduction'] = stripslashes($val['introduction']);
                $val['default_image'] = $val['default_image'] ? $config['qiniu_url'].$val['default_image'] : '';
                $data[$key] = $val;
            }
        } else {
            $this->ajax_return(FLAG_ERROR, '没有更多数据！');
        }
        $this->ajax_return(FLAG_OK, '', $data);
    }

}